package com.dsbj.icardservice.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.icardservice.entity.Reservation;
import com.dsbj.icardservice.entity.ReservationPrimary;
import com.dsbj.icardservice.service.ReservationPrimaryService;
import com.dsbj.icardservice.service.ReservationService;
import com.dsbj.icardservice.utils.strprocess.StrUtil;
import com.dsbj.icardservice.webserviceclient.dsbjpmc.AllowUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/reservation")
public class ReservationController {
	
	@Autowired
	private ReservationService reservationService;
	
	@Resource
	private ReservationPrimaryService reservationPrimaryService;
	
	

	/**
	 * 根据creatorEmployeeNo获取其创建的访客单列表，并分页。
	 *
	 * @param
	 * @return
	 * @author cjw 2019-10-09
	 */
	@RequestMapping("/listByCreatorEmployeeNo")
	public ResponseMessage listByCreatorEmployeeNo(String creatorEmployeeNo, Integer pagesize, Integer pagenum) {
		
		/**
		 * 解析传来的参数
		 */
		if (StrUtil.isEmpty(creatorEmployeeNo)) {
			return ResponseMessage.error("传入的参数有误!");
		}
		if (null == pagesize || pagesize < 1 || null == pagenum || pagenum < 0) {
			return ResponseMessage.error("传入的参数有误!");
		}
		
		/**
		 * 获取其创建的访客单列表，并分页
		 */
		Page<ReservationPrimary> page = reservationPrimaryService.getReservationPrimaryByPage(creatorEmployeeNo, pagesize, pagenum);
		//输出reservationPrimaryPage
		//Page page1=OutputReservationPrimary.outputReservationPrimaryPage(page);
		return ResponseMessage.ok(page);
	}
	
	/**
	 * 例子。
	 * @deprecated 待删
	 * @param id
	 * @return
	 */
	@Deprecated
	@RequestMapping("/getReservationById")
	public List<Reservation> getReservationById(String id) {
		return reservationService.getReservationById(id);
	}
	
	/**
	 * 根据id获取访客预约单详情。
	 * @param
	 * @return
	 * @author cjw 2019-10-09
	 */
	@RequestMapping(value = "/getReservationPrimaryById")
	public ResponseMessage getReservationPrimaryById(String id){
		
		/**
		 * 解析传来的参数
		 */
		if (StrUtil.isEmpty(id)) {
			return ResponseMessage.error("传入的参数有误!");
		}
		
		/**
		 * 根据id获取访客预约单详情。
		 */
		ReservationPrimary reservationPrimary=reservationPrimaryService.getReservationPrimaryById(id);
		return ResponseMessage.ok(reservationPrimary);
	}
	
	@RequestMapping("/getNewReservationNo")
	public ResponseMessage<String> getNewReservationNo() {
		return ResponseMessage.ok(reservationService.getNewReservationno());
	}
	
	@RequestMapping("/save")
	public ResponseMessage<String> save(@RequestBody Map<String, JSONObject> map) {
		AllowUser user = map.get("user").toJavaObject(AllowUser.class);
		ReservationPrimary reservationPrimary = map.get("reservationPrimary").toJavaObject(ReservationPrimary.class);

		if (null == user || null == reservationPrimary) {
			return ResponseMessage.error("传入参数有误");
		}
		return reservationService.save(user, reservationPrimary);
	}
	
	
}
